# -*- coding: utf-8 -*-#
#-------------------------------------------------------------------------------
# 建立者:        黄周云  
# Name:         hmwk01
# Description:
# Author:       ASUS
# Date:         2019/4/17
#-------------------------------------------------------------------------------


'''
作业:请解析如下 json 字符串为 字典
保存为分级 数据文件,能一目了然的看明白
'''


import json
pdDict = {}
pdList = []
try:
    f = open('products.json', 'r', encoding='utf-8')
    fr = f.read()
    Distojb = json.loads(fr)
    dataList = Distojb["data"]
    for i in range(6):
        name = dataList[i]["thd_name"]
        for pdt in dataList[i]["product"]:
            title = pdt["title"]
            pdList.append(title)
        pdDict[name] = pdList
        pdList = []
    f.close()
except Exception as e:
    print(e)
listStr = []
try:
    fp = open('cpshuju.txt', 'w+', encoding='utf-8')
    items = list(pdDict.items())
    for item in items:
        cpname, cp = item
        cp = str(cp)
        listStr.append("{0:<3}:{1:>10}".format(cpname, cp) + '\n')
    fp.writelines(listStr)
    fp.close()
except Exception as e:
            print(e)











